package com.bmslz.monitor.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bmslz.monitor.base.entity.ProjectAlarm;
import com.bmslz.monitor.base.util.StringUtils;
import com.bmslz.monitor.base.dto.EditProjectAlarmModelDto;
import com.bmslz.monitor.base.entity.AlarmModel;
import com.bmslz.monitor.base.entity.Project;
import com.bmslz.monitor.base.vo.MapVo;
import com.bmslz.monitor.mapper.ProjectMapper;
import com.bmslz.monitor.service.IAlarmModelService;
import com.bmslz.monitor.service.IProjectAlarmService;
import com.bmslz.monitor.service.IProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 监控项目
 * @Author: bobo
 * @Date: 2025-08-10
 * @Version: V1.0
 */
@Service
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> implements IProjectService {

    @Autowired
    private IAlarmModelService alarmModelService;
    @Autowired
    @Lazy
    private IProjectAlarmService projectAlarmService;

    @Override
    public Project findByNo(String projectNo) {
        return this.getOne(new LambdaQueryWrapper<Project>().eq(Project::getProjectNo, projectNo), false);
    }

    @Override
    public void editAlarmModel(EditProjectAlarmModelDto dto) {
        dto.check();
        Project project = new Project();
        String alarmModelId = dto.getAlarmModelId();
        String projectId = dto.getProjectId();
        project.setId(projectId);
        if (StringUtils.isEmpty(alarmModelId)) {
            //清空告警模板
            project.setAlarmModelId("");
            project.setAlarmModelNo("");
        } else {
            //修改告警模板
            AlarmModel alarmModel = alarmModelService.getById(alarmModelId);
            project.setAlarmModelId(alarmModel.getId());
            project.setAlarmModelNo(alarmModel.getModelNo());
        }
        baseMapper.updateById(project);
    }

    @Override
    public Long total(Date startTime, Date endTime) {
        LambdaQueryWrapper<Project> wrapper = new LambdaQueryWrapper<Project>();
        if (startTime != null) {
            wrapper.ge(Project::getCreateTime, startTime);
        }
        if (endTime != null) {
            wrapper.le(Project::getCreateTime, endTime);
        }
        return this.count(wrapper);
    }

    @Override
    public void refreshOffline() {
        baseMapper.refreshOffline();
    }

    @Override
    public List<MapVo> projectAlarmGroup() {
        return baseMapper.projectAlarmGroup();
    }

    @Override
    public void refreshAlarmIng(String projectId) {
        Project project = baseMapper.selectById(projectId);
        if (project == null) {
            return;
        }
        List<ProjectAlarm> projectAlarms = projectAlarmService.alarmIngByProjectId(projectId, "");
        String status = "0";
        if (projectAlarms.size() > 0) {
            status = Collections.min(projectAlarms.stream().map(ProjectAlarm::getGrade).collect(Collectors.toList()));
        }
        Project update = new Project();
        update.setId(projectId);
        update.setAlarmIng(status);
        baseMapper.updateById(update);
    }
}
